package cn.com.yusys.yusp.message.service.message.channel.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.yusys.yusp.message.service.message.channel.MobileSendInterface;
import cn.com.yusys.yusp.message.service.message.channel.impl.DefaultMobileSendImpl;

@Service
public class MobileSendService {
	private Logger logger = LoggerFactory.getLogger(MobileSendService.class);
	@Autowired
	private List<MobileSendInterface> mobileSendInterface;
	
	private MobileSendInterface getMobileSend(){
		if(mobileSendInterface.size()>1){
			for(MobileSendInterface send:mobileSendInterface){
				if(send instanceof DefaultMobileSendImpl){
					
				}else{
					return send;
				}
			}
		}
		return mobileSendInterface.get(0);
	}
	public int sendMessage(String userMobileNo,String content,Short sendNum){
		int result = 0;
		try {
			result = getMobileSend().sendMessage(userMobileNo, content);
		} catch (Exception e) {
			result = 1;
		}
		
		if (result == 0) {
			return 0;
		} else {
			logger.error("This is the first time that SMS fails");			
		}

		for (int i = 0; i < sendNum; i++) {
			try {
				result = getMobileSend().sendMessage(userMobileNo, content);
			} catch (Exception e) {
				result = 1;
			}
			
			if (result == 0) {
				return 0;
			} else {				
				logger.error("This is the [" + (i + 1) + "]th time that resend SMS fails");
			}
		}
		return 1;
	}
}